<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          // data: ['累计', '已认证', '新增医生', '取消关注']
          data: ['新增认证医生', '取消关注', '累计注册']
        },
        grid: {
          left: 20,
          right: 30,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: chartData.day,
          axisLabel: {
            rotate: 60
          }
        },
        yAxis: [{
          name: '人/日',
          type: 'value'
        }, {
          name: '累计注册',
          type: 'value'
        }],
        series: [{
          type: 'line',
          yAxisIndex: 1,
          name: '累计注册',
          smooth: true,
          data: chartData.accumulated,
          animationDuration: 2000,
          animationEasing: 'cubicInOut'
        },
        {
          type: 'bar',
          barMaxWidth: 30,
          name: '新增认证医生',
          smooth: true,
          data: chartData.approve,
          animationDuration: 2000,
          animationEasing: 'cubicInOut',
          itemStyle: {
            color: '#ec7644'
          }
        },
        // {
        //   type: 'bar',
        //   name: '已认证',
        //   stack: 'num',
        //   smooth: true,
        //   data: chartData.approve,
        //   animationDuration: 2000,
        //   animationEasing: 'cubicInOut'
        // },
        // {
        //   type: 'bar',
        //   name: '新增医生',
        //   stack: 'num',
        //   smooth: true,
        //   data: chartData.newDoctor,
        //   animationDuration: 2000,
        //   animationEasing: 'cubicInOut'
        // },
        {
          type: 'line',
          name: '取消关注',
          smooth: true,
          data: chartData.unfollow,
          animationDuration: 2000,
          animationEasing: 'cubicInOut'
        }]
      })
    }
  }
}
</script>
